#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify
from service.commentInfoService import CommentInfoService
from utils import commons
from utils.loggings import loggings
from utils.response_code import RET


class CommentInfoOtherResource(Resource):

    # join table query
    @classmethod
    def joint_query(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('CommentPostID', type=int, location='args', required=False, help='CommentPostID参数类型不正确或缺失')
        parser.add_argument('CommentUserID', type=str, location='args', required=False, help='CommentUserID参数类型不正确或缺失')
        parser.add_argument('CommentContent', type=str, location='args', required=False, help='CommentContent参数类型不正确或缺失')
        parser.add_argument('CommentParent', type=int, location='args', required=False, help='CommentParent参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='args', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('CreateTime', type=str, location='args', required=False, help='CreateTime参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='args', required=False, help='ModifyTime参数类型不正确或缺失')
        
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')
        
        res = CommentInfoService.joint_query(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'], totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def getallcomment(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('CommentPostID', type=int, location='args', required=True, help='CommentPostID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')


        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = CommentInfoService.getallcomment(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], comment=res['data'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def addcomment(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('CommentPostID', type=int, location='form', required=True, help='CommentPostID参数类型不正确或缺失')
        parser.add_argument('CommentUserID', type=str, location='form', required=True, help='CommentUserID参数类型不正确或缺失')
        parser.add_argument('CommentContent', type=str, location='form', required=True,help='CommentContent参数类型不正确或缺失')
        parser.add_argument('CommentParent', type=int, location='form', required=False, help='CommentParent参数类型不正确或缺失')
        parser.add_argument('CommentRoot', type=int, location='form', required=False, help='CommentParent参数类型不正确或缺失')


        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = CommentInfoService.addcomment(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def messageinform(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = CommentInfoService.messageinform(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                          )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])
